/*
 *  DbTableEntry.cpp
 *  StrategyInspector
 *
 *  Created by jiro on 12/08/07.
 *  Copyright 2012 jiro music. All rights reserved.
 *
 */

#include "DbTableEntry.h"
#include "../../Commons.h"

NSString* DbTableEntry::tableName()
{
	throw ( [ NSException exceptionWithName: kText_NeedInheritClassImplementation reason: @""  userInfo: nil ] ); 
}

NSString* DbTableEntry::queryCreateTable()
{
	throw ( [ NSException exceptionWithName: kText_NeedInheritClassImplementation reason: @""  userInfo: nil ] ); 
}

NSString* DbTableEntry::queryFieldsOfCreateTable()
{
	throw ( [ NSException exceptionWithName: kText_NeedInheritClassImplementation reason: @""  userInfo: nil ] ); 
}

NSString* DbTableEntry::queryInsertInitialValues()
{
	throw ( [ NSException exceptionWithName: kText_NeedInheritClassImplementation reason: @""  userInfo: nil ] ); 
}

NSString* DbTableEntry::querySelectAll()
{
	throw ( [ NSException exceptionWithName: kText_NeedInheritClassImplementation reason: @""  userInfo: nil ] ); 
}

NSString* DbTableEntry::querySelectAll( const std::vector< ClauseNode >& clauses )
{
	throw ( [ NSException exceptionWithName: kText_NeedInheritClassImplementation reason: @""  userInfo: nil ] ); 
}

NSString* DbTableEntry::querySelectAll( NSString* tableName, const std::vector< ClauseNode >& clauses )
{
	NSMutableString* c = [ NSMutableString string ];
	for ( int i = 0, count = clauses.size(); i < count; i++ )
	{
		const ClauseNode& clause = clauses[ i ];
		[ c appendFormat: @" %@ ", clause.query() ];
	}
	NSString* s = [ NSString stringWithFormat: @"select * from %@ %@;", tableName, c ];
	return s;
}

NSString* DbTableEntry::querySelect( const std::vector< FieldNode >& fieldNodes, const std::vector< ClauseNode >& clauses )
{
	throw ( [ NSException exceptionWithName: kText_NeedInheritClassImplementation reason: @""  userInfo: nil ] ); 
}

NSString* DbTableEntry::queryInsertWithSerialValue() const
{
	throw ( [ NSException exceptionWithName: kText_NeedInheritClassImplementation reason: @""  userInfo: nil ] ); 
}

NSString* DbTableEntry::queryInsertValueWithSerialValue() const
{
	throw ( [ NSException exceptionWithName: kText_NeedInheritClassImplementation reason: @""  userInfo: nil ] ); 
}

NSString* DbTableEntry::querySelect( const std::vector< FieldNode >& fieldNodes, NSString* tableName, const std::vector< ClauseNode >& clauses )
{
	NSMutableString* c = [ NSMutableString string ];
	for ( int i = 0, count = clauses.size(); i < count; i++ )
	{
		const ClauseNode& clause = clauses[ i ];
		[ c appendFormat: @" %@ ", clause.query() ];
	}
	
	NSMutableString* fields = [ NSMutableString string ];
	for ( int i = 0, count = fieldNodes.size(); i < count; i++ )
	{
		const FieldNode& node = fieldNodes[ i ];
		[ fields appendString: node.query() ];
		if ( i != count - 1 )
		{
			[ fields appendString: @", " ];
		}
	}
	NSString* s = [ NSString stringWithFormat: @"select %@ from %@ %@;", fields, tableName, c ];
	return s;
}
